#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow ( QWidget* parent ) :
    QMainWindow ( parent ),
    ui ( new Ui::MainWindow )
{
    ui->setupUi ( this );

    m0 = new QQtFindServerManager ( this );
    connect ( m0, SIGNAL ( signalFindServer ( QString, int ) ),
              this, SLOT ( slotFindServer ( QString, int ) ) );
    m0->setServerPort ( 5522 );

    m1 = new QQtUserOnlineManager ( this );
    connect ( m1, SIGNAL ( signalServerInformation ( QQtDictionary ) ),
              this, SLOT ( slotServerInformation ( QQtDictionary ) ) );
    connect ( m1, SIGNAL ( signalUserInformation ( QQtDictionary ) ),
              this, SLOT ( slotUserInformation ( QQtDictionary ) ) );
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slotUserInformation ( const QQtDictionary& dictUser )
{
    QByteArray bytes = dictUser.toJson ( QJsonDocument::Indented );
    ui->textBrowser->clear();
    ui->textBrowser->append ( bytes );
}

void MainWindow::slotServerInformation ( const QQtDictionary& dictServer )
{
    QByteArray bytes = dictServer.toJson ( QJsonDocument::Indented );
    ui->textBrowser->clear();
    ui->textBrowser->append ( bytes );
}

void MainWindow::slotFindServer ( QString ip, int port )
{
    m1->setServerAddress ( QHostAddress ( ip ), port );
}

void MainWindow::on_pushButton_3_clicked ( bool checked )
{
    m0->sendFindServer();
}

void MainWindow::on_pushButton_clicked ( bool checked )
{
    m1->sendRequestServerInformation();
}

void MainWindow::on_pushButton_2_clicked ( bool checked )
{
    m1->sendRequestUserInformation();
}

void MainWindow::on_pushButton_4_clicked ( bool checked )
{
    m1->sendInformation();
}

void MainWindow::on_pushButton_5_clicked ( bool checked )
{
    //logon
    m1->sendLogin();
}

void MainWindow::on_pushButton_6_clicked ( bool checked )
{
    //logout
    m1->sendLogout();
}
